package com.ruoyi.outcome.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.outcome.domain.vo.ResearchAwardTrack;
import com.ruoyi.outcome.domain.vo.ResearchOutcomeTrack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.outcome.domain.ResearchAward;
import com.ruoyi.outcome.service.IResearchAwardService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 科研奖励管理Controller
 *
 * @author zxh
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/award")
public class ResearchAwardController extends BaseController
{
    @Autowired
    private IResearchAwardService researchAwardService;

    /**
     * 查询科研奖励管理列表
     */
    @RequiresPermissions("outcome:award:list")
    @GetMapping("/list")
    public TableDataInfo list(ResearchAward researchAward)
    {
        startPage();
        researchAward.setIsDeleted(0);
        List<ResearchAward> list = researchAwardService.selectResearchAwardList(researchAward);
        return getDataTable(list);
    }

    /**
     * 导出科研奖励管理列表
     */
    @RequiresPermissions("outcome:award:export")
    @Log(title = "科研奖励管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResearchAward researchAward)
    {
        List<ResearchAward> list = researchAwardService.selectResearchAwardList(researchAward);
        ExcelUtil<ResearchAward> util = new ExcelUtil<ResearchAward>(ResearchAward.class);
        util.exportExcel(response, list, "科研奖励管理数据");
    }

    /**
     * 获取科研奖励管理详细信息
     */
    @RequiresPermissions("outcome:award:query")
    @GetMapping(value = "/{awardId}")
    public AjaxResult getInfo(@PathVariable("awardId") Long awardId)
    {

        return success(researchAwardService.selectResearchAwardByAwardId(awardId));
    }

    /**
     * 新增科研奖励管理
     */
    @RequiresPermissions("outcome:award:add")
    @Log(title = "科研奖励管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResearchAward researchAward)
    {
        researchAward.setCreatedBy(SecurityUtils.getUsername());

        ResearchAwardTrack awardTrack= new ResearchAwardTrack();
        BeanUtils.copyProperties(researchAward, awardTrack);
        awardTrack.setActionType("ADD");
        researchAwardService.insertResearchAwardAudit(awardTrack);
        return toAjax(researchAwardService.insertResearchAward(researchAward));
    }

    /**
     * 修改科研奖励管理
     */
    @RequiresPermissions("outcome:award:edit")
    @Log(title = "科研奖励管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResearchAward researchAward)
    {
        researchAward.setUpdatedBy(SecurityUtils.getUsername());
        researchAward.setUpdatedAt(new Date());

        ResearchAwardTrack awardTrack= new ResearchAwardTrack();
        BeanUtils.copyProperties(researchAward, awardTrack);
        awardTrack.setActionType("UPDATE");
        researchAwardService.insertResearchAwardAudit(awardTrack);
        return toAjax(researchAwardService.updateResearchAward(researchAward));
    }

    /**
     * 删除科研奖励管理
     */
    @RequiresPermissions("outcome:award:remove")
    @Log(title = "科研奖励管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{awardIds}")
    public AjaxResult remove(@PathVariable Long[] awardIds)
    {
        for(Long awardId:awardIds)
        {
            ResearchAward researchAward = researchAwardService.selectResearchAwardByAwardId(awardId);
            researchAward.setIsDeleted(1);
            researchAward.setDeletedBy(SecurityUtils.getUsername());
            researchAward.setDeletedAt(new Date());
            researchAwardService.updateResearchAward(researchAward);

            ResearchAwardTrack awardTrack= new ResearchAwardTrack();
            BeanUtils.copyProperties(researchAward, awardTrack);
            awardTrack.setActionType("DELETE");
            researchAwardService.insertResearchAwardAudit(awardTrack);
        }
        return success();
//        return toAjax(researchAwardService.deleteResearchAwardByAwardIds(awardIds));
    }



    @GetMapping(value = "/awardtrack/{awardId}")
    public AjaxResult getAwardTrack(@PathVariable("awardId") Long awardId)
    {
        return success(researchAwardService.selectResearchAwardAuditList(awardId));
    }
}
